<?php
class Warehouse_Model_Shelf
{

	private $_table;
	//Initalizing Db Table  
	public function __construct(){
		$this->_table = new Warehouse_Model_DbTable_Shelf();
	}
	
	
	/**
	*Create A Record  
	*@param rowSet $row Warehouse_Shelf's data 
	*@return boolean
	*/
	public  function create($row){	 
 		return $this->_table->insert($row); 
	}
	
	/**
	*Update One Row  
	*@param rowSet $row Warehouse_Shelf's data 
	*@param int $ws_id 
	*@return boolean
	*/
	public  function update($row,$id){
	
		$where = $this->_table->getAdapter()->quoteInto("ws_id= ?", $id);
 
		return $this->_table->update($row, $where);
		
	}
	
	/**
	*根据区域修改
	*/
	public  function updateByWaCode($row,$code){
		$where = $this->_table->getAdapter()->quoteInto("wa_code= ?", $code);
		return $this->_table->update($row, $where);
	}
		
	/**
	*Delete One Row
	*@param int $ws_id 
	*@return boolean
	*/
	public function delete($id){
	
		$where = $this->_table->getAdapter()->quoteInto("ws_id= ?", $id);	 
 
		return $this->_table->delete($where);
		
	}	

    public function getByCondition($condition,$type='*',$orderby=array('ws_id'),$pageSize=0,$page=1){
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table,$type);
        $select->where("1 =?",1);
		if(isset($condition["ws_code"]) && $condition["ws_code"]!=''){
            $select->where(" ws_code = ?",$condition["ws_code"]);
        }
		if(isset($condition["wh_code"]) && $condition["wh_code"]!=''){
            $select->where(" wh_code = ?",$condition["wh_code"]);
        }
		if(isset($condition["wa_code"]) && $condition["wa_code"]!=''){
            $select->where(" wa_code = ?",$condition["wa_code"]);
        }
		if(isset($condition["warehouse_id"]) && $condition["warehouse_id"]!=''){
            $select->where(" warehouse_id = ?",$condition["warehouse_id"]);
        }
        if('count(*)'==$type){
            return $this->_table->getAdapter()->fetchOne($select);
        }else{
            $select->order($orderby);
            if($pageSize>0 and $page>0)
            {
                $start =($page-1)*$pageSize;
                $select->limit($pageSize,$start);
            }
			$sql=$select->__toString();
		    return $this->_table->getAdapter()->fetchAll($sql);
        }
		
	}	
	
    public function getByJoinCondition($condition=array(),$type='*',$orderby=array('ws.ws_id'),$pageSize=0,$page=0){
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table." as ws",$type);
		$select->joinLeft('warehouse_area', 'ws.wa_code = warehouse_area.wa_code', 'warehouse_area.wa_name');
		$select->joinLeft('warehouse_products', 'ws.ws_code = warehouse_products.ws_code', array('warehouse_products.product_id','warehouse_products.wp_quantity','warehouse_products.wp_quantity_hold','warehouse_products.ws_code as wp_ws_code','warehouse_products.wp_status','warehouse_products.wh_last_update'));
        $select->where("1 =?",1);
		if(isset($condition["ws_code"]) && $condition["ws_code"]!=''){
            $select->where(" ws.ws_code = ?",$condition["ws_code"]);
        }
        if(isset($condition["ws_code_like"]) && $condition["ws_code_like"]!=''){
            $select->where(" ws.ws_code like '".$condition["ws_code_like"]."%'");
        }
		if(isset($condition["wh_code"]) && $condition["wh_code"]!=''){
            $select->where(" ws.wh_code = ?",$condition["wh_code"]);
        }
		if(isset($condition["wa_code"]) && $condition["wa_code"]!=''){
            $select->where(" ws.wa_code = ?",$condition["wa_code"]);
        }
		if(isset($condition["warehouse_id"]) && $condition["warehouse_id"]!=''){
            $select->where(" ws.warehouse_id = ?",$condition["warehouse_id"]);
        }
		if(isset($condition["product_id"]) && $condition["product_id"]!=''){
            $select->where(" warehouse_products.product_id = ?",$condition["product_id"]);
        }
		if(isset($condition["isEmpty"]) && $condition["isEmpty"]!=''){
			if($condition["isEmpty"]=='1'){
				$select->where(" warehouse_products.ws_code!=''");
			}else{
				$select->where(" warehouse_products.ws_code is null");
			}
        }
		if(isset($condition["customer_id"]) && $condition["customer_id"]!=''){
            $select->where(" warehouse_products.customer_id = ?",$condition["customer_id"]);
        }
		if(isset($condition["poc_id"]) && $condition["poc_id"]!=''){
            $select->where(" warehouse_area.poc_id = ?",$condition["poc_id"]);
        }
        if('count(*)'==$type){
            return $this->_table->getAdapter()->fetchOne($select);
        }else{
            $select->order($orderby);
            if($pageSize>0 and $page>0)
            {
                $start =($page-1)*$pageSize;
                $select->limit($pageSize,$start);
            }
			$sql=$select->__toString();
		    return $this->_table->getAdapter()->fetchAll($sql);
        }
		
	}	

    public function getByJoinChooseCondition($condition=array(),$type='*',$orderby=array('ws.ws_code asc'),$pageSize=0,$page=0){
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table." as ws",$type);
		$select->joinLeft('warehouse_area', 'ws.wa_code = warehouse_area.wa_code', 'warehouse_area.wa_name');
		$select->joinLeft('warehouse_products', 'ws.ws_code = warehouse_products.ws_code', array('warehouse_products.product_id'));
        $select->where("1 =?",1);
		if(isset($condition["ws_code"]) && $condition["ws_code"]!=''){
            $select->where(" ws.ws_code = ?",$condition["ws_code"]);
        }
		if(isset($condition["wh_code"]) && $condition["wh_code"]!=''){
            $select->where(" ws.wh_code = ?",$condition["wh_code"]);
        }
		if(isset($condition["wa_code"]) && $condition["wa_code"]!=''){
            $select->where(" ws.wa_code = ?",$condition["wa_code"]);
        }
		if(isset($condition["wa_type"]) && $condition["wa_type"]!=''){
            $select->where(" warehouse_area.wa_type = ?",$condition["wa_type"]);
        }
		if(isset($condition["warehouse_id"]) && $condition["warehouse_id"]!=''){
            $select->where(" ws.warehouse_id = ?",$condition["warehouse_id"]);
        }
		if(isset($condition["poc_id"]) && $condition["poc_id"]!=''){
            $select->where(" warehouse_area.poc_id =".$condition["poc_id"]." or warehouse_area.poc_id=0");
			$select->where(" warehouse_products.product_id = ".$condition["product_id"]." or warehouse_products.wp_status = 0 or warehouse_products.ws_code is null");
        }
		if(isset($condition["status"]) && $condition["status"]!=''){
            $select->where(" warehouse_area.wa_status = ?",$condition["status"]);
			$select->where(" ws.ws_statu = ?",$condition["status"]);
        }
        if(isset($condition["rand"]) && $condition["rand"]!=''){
            $select->where(" ws.ws_id >= ?",$condition["rand"]);
        }
        if('count(*)'==$type){
            return $this->_table->getAdapter()->fetchOne($select);
        }else{
            $select->order($orderby);
            if($pageSize>0 and $page>0)
            {
                $start =($page-1)*$pageSize;
                $select->limit($pageSize,$start);
            }
			$sql=$select->__toString();
		    return $this->_table->getAdapter()->fetchAll($sql);
        }
		
	}	
	
	/**
	*Get Rowset By Warehouse_Shelf 
	*@param string $ws_id 
	*@return Rowset
	*/
	public function getById($id){
		$where = $this->_table->getAdapter()->quoteInto('ws_id= ?', $id);
		return $this->_table->fetchRow($where);
	}
	
	public function getByfield($field=1,$value=1){
		$where = $this->_table->getAdapter()->quoteInto("{$field} = ?",$value);	
		return $this->_table->fetchRow($where);
	}

    public function getBySortInWscode($wscode,$type='*',$orderby=array('ws_sort desc')){
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table,$type);
        $select->where(" ws_code in(?)",$wscode);
        $select->order($orderby);
        $sql=$select->__toString();
        return $this->_table->getAdapter()->fetchAll($sql);
    }
	
	public function getWscodeByWacode($wacode,$type='*',$orderby=array('ws_sort desc')){
		$select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table,$type);
        $select->where(" wa_code in(?)",$wacode);
        $select->order($orderby);
        $sql=$select->__toString();
        return $this->_table->getAdapter()->fetchAll($sql);
	}
	
    public function getByJoinClass($ws_code,$type='*'){
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table,$type);
        $select->joinLeft('warehouse_shelf_class', 'warehouse_shelf.wsc_code = warehouse_shelf_class.wsc_code');
        $select->where("warehouse_shelf.ws_code=?",$ws_code);
        $sql=$select->__toString();
        return $this->_table->getAdapter()->fetchRow($sql);
    }

    public function getByRandShelf($condition=array(),$type=array('ws_id'),$orderby=array('ws_sort desc','ws_id asc')){
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table,$type);
        $select->where("ws_id >=(SELECT floor(RAND() * (SELECT MAX(ws_id) FROM ".$table.")))");
        if(isset($condition["wa_code"]) && $condition["wa_code"]!=''){
            $select->where("wa_code in(?)",$condition["wa_code"]);
        }
        $select->order($orderby);
        $select->limit(1);
        $sql=$select->__toString();
        return $this->_table->getAdapter()->fetchRow($sql);
    }

    /*
    * warehouse_shelf left join warehouse_products
    */
    public function getByLeftJoinWarehouseProductsCondition($condition=array(),$type='*',$pageSize=0,$page=0,$orderby=array('ws.ws_code asc')){
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table,$type);
        $select->joinLeft('warehouse_products', 'warehouse_shelf.ws_code = warehouse_products.ws_code',array('warehouse_products.product_id','warehouse_products.customer_id','warehouse_products.wp_status','warehouse_products.wp_quantity','warehouse_products.wp_quantity_hold'));
        $select->where("1 =?",1);
        if(isset($condition["ws_code"]) && $condition["ws_code"]!=''){
            $select->where(" warehouse_shelf.ws_code = ?",$condition["ws_code"]);
        }
        if(isset($condition["wh_code"]) && $condition["wh_code"]!=''){
            $select->where(" warehouse_shelf.wh_code = ?",$condition["wh_code"]);
        }
        //分区 in() array()
        if(isset($condition["wa_code"]) && $condition["wa_code"]!=''){
            $select->where(" warehouse_shelf.wa_code in(?)",$condition["wa_code"]);
        }
        if(isset($condition["wsc_code"]) && $condition["wsc_code"]!=''){
            $select->where(" warehouse_shelf.wsc_code = ?",$condition["wsc_code"]);
        }
        if(isset($condition["warehouse_id"]) && $condition["warehouse_id"]!=''){
            $select->where(" warehouse_shelf.warehouse_id = ?",$condition["warehouse_id"]);
        }
        //推荐货架
        if(isset($condition["recommend"]) && $condition["recommend"]!=''){
            if(isset($condition["product_id"]) && $condition["product_id"]!=''){
                $select->where(" warehouse_products.product_id = ".$condition["product_id"]." or warehouse_products.wp_status = 0 or warehouse_products.ws_code is null");
            }else{
                $select->where(" warehouse_products.wp_status = 0 or warehouse_products.ws_code is null");
            }
        }elseif(isset($condition["product_id"]) && $condition["product_id"]!=''){
            $select->where(" warehouse_products.product_id = ".$condition["product_id"]);
        }
        //随机ID
        if(isset($condition["rand"]) && $condition["rand"]!=''){
            $select->where(" warehouse_shelf.ws_id >= ?",$condition["rand"]);
        }

        if(isset($condition["ws_statu"]) && $condition["ws_statu"]!=''){
            $select->where(" warehouse_shelf.ws_statu = ?",$condition["ws_statu"]);
        }
        if(isset($condition["wp_status"]) && $condition["wp_status"]!=''){
            $select->where(" warehouse_products.wp_status = ?",$condition["wp_status"]);
        }
        if('count(*)'==$type){
            return $this->_table->getAdapter()->fetchOne($select);
        }else{
            $select->order($orderby);
            if($pageSize>0 and $page>0)
            {
                $start =($page-1)*$pageSize;
                $select->limit($pageSize,$start);
            }
            $sql=$select->__toString();
            return $this->_table->getAdapter()->fetchAll($sql);
        }

    }
}